//
//  AreaAlertView.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/13.
//

#import "AreaAlertView.h"
#import "HomeGetDetailAllModel.h"
#import "HomeChooseAreaAlertTableViewCell.h"




@interface AreaAlertView ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger Index;
    UIView * topV;
}
@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) UITableView * oneTableView;
@property (nonatomic,strong) NSMutableArray * oneDataSouce;
@property (nonatomic,strong) UITableView * twoTableView;
@property (nonatomic,strong) NSMutableArray * twoDataSouce;
@property (nonatomic,strong) UITableView * therrTableView;
@property (nonatomic,strong) NSMutableArray * therrDataSouce;
@property (nonatomic,strong) HomeGetDetailAllModel * model;




@end

@implementation AreaAlertView



- (instancetype)init
{
    self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        
        [self setUpUI];
        
    }
    return self;
}

-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [self.oneDataSouce addObjectsFromArray:dataSource];
    [self.oneTableView reloadData];
}




-(void)setUpUI{
    
    
    topV = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT , SCREEN_WIDTH, 40)];
    topV.backgroundColor = COLOR_RGBA(242, 242, 242, 1.0);
    [self addSubview:topV];

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, 0, 100, 40);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [cancelBtn addTarget:self action:@selector(respondsTo_checkCancel) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:cancelBtn];

    UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    yesBtn.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 100, 40);
    [yesBtn setTitle:@"完成" forState:UIControlStateNormal];
    [yesBtn setTitleColor:COLOR_RGBA(69, 186, 155, 1) forState:UIControlStateNormal];
    [yesBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [yesBtn addTarget:self action:@selector(respondsTo_checkyes) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:yesBtn];
    
    
    self.follewView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207)];
    self.follewView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.follewView];
    
    [self.follewView addSubview:self.oneTableView];
    [self.follewView addSubview:self.twoTableView];
    [self.follewView addSubview:self.therrTableView];
    
    [self.oneTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.follewView.mas_top);
        make.bottom.left.equalTo(self.follewView);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    [self.twoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.oneTableView.mas_right);
        make.bottom.equalTo(self.follewView);
        make.top.equalTo(self.oneTableView.mas_top);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    [self.therrTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.oneTableView.mas_top);
        make.left.equalTo(self.twoTableView.mas_right);
        make.bottom.equalTo(self.follewView);
        make.width.equalTo(@(SCREEN_WIDTH / 3));
    }];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        topV.frame = CGRectMake(0, SCREEN_HEIGHT - 247, SCREEN_WIDTH, 40);
        self.follewView.frame = CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207);
    }];
    
    
    
    
}

#pragma amrk -- 点击事件
-(void)respondsTo_checkCancel{
    [self remove];
}

-(void)respondsTo_checkyes{
    [self remove];

    
    
    
    NSMutableArray * chooseArr = @[].mutableCopy;
    NSMutableArray * showArr = @[].mutableCopy;
    
    
    [self.dataSource enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj1, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableArray * obj1Arr = @[].mutableCopy;
        if (obj1.allSelect){
            
            NSMutableDictionary * dic = @{}.mutableCopy;
            [dic setValue:obj1.name forKey:@"name"];
            [dic setValue:obj1.id forKey:@"id"];
            [dic setValue:obj1.code forKey:@"code"];
            [dic setValue:obj1.category forKey:@"category"];
            [dic setValue:obj1.parentId forKey:@"parentId"];
            
            [obj1Arr addObject:dic];
            [chooseArr addObject:obj1Arr];
            
            
            
            [showArr addObject:obj1.name];
        }
        
        [obj1.children enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * _Nonnull obj2, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMutableArray * obj2Arr = @[].mutableCopy;
            if (obj2.allSelect){
                
                
                NSMutableDictionary * dic = @{}.mutableCopy;
                [dic setValue:obj1.name forKey:@"name"];
                [dic setValue:obj1.id forKey:@"id"];
                [dic setValue:obj1.code forKey:@"code"];
                [dic setValue:obj1.category forKey:@"category"];
                [dic setValue:obj1.parentId forKey:@"parentId"];
                
                
                NSMutableDictionary * dic2 = @{}.mutableCopy;
                [dic2 setValue:obj2.name forKey:@"name"];
                [dic2 setValue:obj2.id forKey:@"id"];
                [dic2 setValue:obj2.code forKey:@"code"];
                [dic2 setValue:obj2.category forKey:@"category"];
                [dic2 setValue:obj2.parentId forKey:@"parentId"];
                
                [obj2Arr addObject:dic];
                [obj2Arr addObject:dic2];
                [chooseArr addObject:obj2Arr];
                [showArr addObject:[NSString stringWithFormat:@"%@-%@",obj1.name,obj2.name]];
            }
            
            [obj2.children enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * _Nonnull obj3, NSUInteger idx, BOOL * _Nonnull stop) {
                NSMutableArray * obj3Arr = @[].mutableCopy;
                if (obj3.allSelect){
                    
                    
                    NSMutableDictionary * dic = @{}.mutableCopy;
                    [dic setValue:obj1.name forKey:@"name"];
                    [dic setValue:obj1.id forKey:@"id"];
                    [dic setValue:obj1.code forKey:@"code"];
                    [dic setValue:obj1.category forKey:@"category"];
                    [dic setValue:obj1.parentId forKey:@"parentId"];
                    
                    
                    NSMutableDictionary * dic2 = @{}.mutableCopy;
                    [dic2 setValue:obj2.name forKey:@"name"];
                    [dic2 setValue:obj2.id forKey:@"id"];
                    [dic2 setValue:obj2.code forKey:@"code"];
                    [dic2 setValue:obj2.category forKey:@"category"];
                    [dic2 setValue:obj2.parentId forKey:@"parentId"];
                    
                    NSMutableDictionary * dic3 = @{}.mutableCopy;
                    [dic3 setValue:obj3.name forKey:@"name"];
                    [dic3 setValue:obj3.id forKey:@"id"];
                    [dic3 setValue:obj3.code forKey:@"code"];
                    [dic3 setValue:obj3.category forKey:@"category"];
                    [dic3 setValue:obj3.parentId forKey:@"parentId"];
                    
                    [obj3Arr addObject:dic];
                    [obj3Arr addObject:dic2];
                    [obj3Arr addObject:dic3];
                    [chooseArr addObject:obj3Arr];
                    [showArr addObject:[NSString stringWithFormat:@"%@-%@-%@",obj1.name,obj2.name,obj3.name]];
                }
            }];
            
        }];
    }];
    
    
    // chooseArr 后台需要的数据
    
    
    
    
    _Areablock(chooseArr,showArr);
    
}







- (void)remove {

    [UIView animateWithDuration:0.25 animations:^{

        self->topV.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 40);
        self.follewView.frame = CGRectMake(0, CGRectGetMaxY(self->topV.frame), SCREEN_WIDTH, 207);

    } completion:^(BOOL finished) {

        [self removeFromSuperview];
    }];

}





#pragma mark  -- UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (tableView == self.oneTableView){
            return self.oneDataSouce.count;
        }else if (tableView == self.twoTableView){
            return self.twoDataSouce.count;
        }else{
            return self.therrDataSouce.count;
        }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
       
    HomeChooseAreaAlertTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeChooseAreaAlertTableViewCell"];
    
    if (!cell){
        cell = [[HomeChooseAreaAlertTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeChooseAreaAlertTableViewCell"];
        
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HomeGetDetailAllModel * model ;
    if (tableView == self.oneTableView){
       model = self.oneDataSouce[indexPath.row];
        
        if (model.allSelect){
            [cell.chooseBtn setImage:[UIImage imageNamed:@"选择"] forState:(UIControlStateNormal)];
        }else{
            [cell.chooseBtn setImage:[UIImage imageNamed:@"未选"] forState:(UIControlStateNormal)];
        }
        
//        __block HomeChooseAreaAlertTableViewCell * blcokCell = cell;
        
        cell.chooseDispatchBlock = ^{
            
            NSLog(@"点击了 === %ld",indexPath.row);
            
            [self.twoDataSouce removeAllObjects];
            [self.therrDataSouce removeAllObjects];
            
            if (model.allSelect){ // 取消选中
                model.allSelect = NO;
                [model.children enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = NO;
                    [self.twoDataSouce addObject:[HomeGetDetailAllModel mj_objectWithKeyValues:obj]];
                }];
                [self.twoDataSouce enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = NO;
                }];
                
                [self.therrDataSouce enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = NO;
                }];
                
                
            }else{ // 选中
                model.allSelect = YES;
                
                [model.children enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = YES;
                    [self.twoDataSouce addObject:[HomeGetDetailAllModel mj_objectWithKeyValues:obj]];
                }];
                [self.twoDataSouce enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = YES;
                }];
                
                [self.therrDataSouce enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = YES;
                }];
                
                
                
            }
            [self.oneTableView reloadData];
            [self.twoTableView reloadData];
            [self.therrTableView reloadData];
        };
        
    }
    if (tableView == self.twoTableView){
       model = self.twoDataSouce[indexPath.row];
        
        if (model.allSelect){
            [cell.chooseBtn setImage:[UIImage imageNamed:@"选择"] forState:(UIControlStateNormal)];
        }else{
            [cell.chooseBtn setImage:[UIImage imageNamed:@"未选"] forState:(UIControlStateNormal)];
        }
        
        
        cell.chooseDispatchBlock = ^{
            
            NSLog(@"点击了 === %ld",indexPath.row);
            
            [self.therrDataSouce removeAllObjects];
            
            if (model.allSelect){ // 取消选中
                model.allSelect = NO;
                [model.children enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = NO;
                    [self.therrDataSouce addObject:[HomeGetDetailAllModel mj_objectWithKeyValues:obj]];
                }];
                
            }else{ // 选中
                model.allSelect = YES;
                
                [model.children enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    obj.allSelect = YES;
                    [self.therrDataSouce addObject:[HomeGetDetailAllModel mj_objectWithKeyValues:obj]];
                    
                }];
            }
            [self.twoTableView reloadData];
            [self.therrTableView reloadData];
        };
        
        
    }
    if (tableView == self.therrTableView){
       model = self.therrDataSouce[indexPath.row];
        
        if (model.allSelect){
            [cell.chooseBtn setImage:[UIImage imageNamed:@"选择"] forState:(UIControlStateNormal)];
        }else{
            [cell.chooseBtn setImage:[UIImage imageNamed:@"未选"] forState:(UIControlStateNormal)];
        }
        
//        __block HomeChooseAreaAlertTableViewCell * blcokCell = cell;
        
        cell.chooseDispatchBlock = ^{

            if (model.allSelect){ // 取消选中
                model.allSelect = NO;
                
            }else{ // 选中
                model.allSelect = YES;
            }
            [self.therrTableView reloadData];
        };
        
    }
    cell.areaLabel.text = model.name;
    if (model.children.count > 0){
        cell.img.image = [UIImage imageNamed:@"向右箭头"];
    }else{
        cell.img.image = [UIImage imageNamed:@""];
    }
    
        
        return  cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(45);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.oneTableView){
        [self.therrDataSouce removeAllObjects];
        HomeGetDetailAllModel * model = self.oneDataSouce[indexPath.row];
        self.twoDataSouce = [NSMutableArray arrayWithArray:[HomeGetDetailAllModel mj_objectArrayWithKeyValuesArray:model.children]];
        [self.twoTableView reloadData];
        [self.therrTableView reloadData];
    }else if (tableView == self.twoTableView){
        
        HomeGetDetailAllModel * model = self.twoDataSouce[indexPath.row];
        self.therrDataSouce = [NSMutableArray arrayWithArray:[HomeGetDetailAllModel mj_objectArrayWithKeyValuesArray:model.children]];
        [self.therrDataSouce enumerateObjectsUsingBlock:^(HomeGetDetailAllModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.allSelect = model.allSelect;
        }];
        
        [self.therrTableView reloadData];
    }
    
}



#pragma amrk -- 懒加载
-(UITableView *)oneTableView{
    if(!_oneTableView){
        _oneTableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _oneTableView.dataSource = self;
        _oneTableView.delegate = self;
        _oneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _oneTableView.showsVerticalScrollIndicator = false;
        _oneTableView.showsHorizontalScrollIndicator = false;
    }
    return _oneTableView;
}
-(UITableView *)twoTableView{
    if(!_twoTableView){
        _twoTableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _twoTableView.dataSource = self;
        _twoTableView.delegate = self;
        _twoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _twoTableView.showsVerticalScrollIndicator = false;
        _twoTableView.showsHorizontalScrollIndicator = false;
    }
    return _twoTableView;
}
-(UITableView *)therrTableView{
    if(!_therrTableView){
        _therrTableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _therrTableView.dataSource = self;
        _therrTableView.delegate = self;
        _therrTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _therrTableView.showsVerticalScrollIndicator = false;
        _therrTableView.showsHorizontalScrollIndicator = false;
    }
    return _therrTableView;
}

-(NSMutableArray *)oneDataSouce{
    if (!_oneDataSouce){
        _oneDataSouce = [NSMutableArray array];
    }
    return _oneDataSouce;
}

-(NSMutableArray *)twoDataSouce{
    if (!_twoDataSouce){
        _twoDataSouce = [NSMutableArray array];
    }
    return _twoDataSouce;
}

-(NSMutableArray *)therrDataSouce{
    if (!_therrDataSouce){
        _therrDataSouce = [NSMutableArray array];
    }
    return _therrDataSouce;
}


@end
